<?php
apf_require_class("Bll_BLL");
apf_require_class('Bll_Attendance_DailyReport');
class Bll_Attendance_Apply extends BLL {

    private $passurl = "";
    public function apply($data) {
        $flag = false;
        if(empty($data) && ! is_array($data)) {
            return $flag;
        }
        $rs = $this->get_default_dao()->insert($data);
        if(!$rs) {
            return $flag;
        } else {
            //拆分数据存入库
            $daily_bll = new Bll_Attendance_DailyReport();
            $ck_status =1;
            if(!empty($data['status']) && $data['status']=='已批准'){
                $ck_status=100;//先写入再批准
            }
            $daily_bll->make_leave_data($rs,$ck_status);
            //如果为年假 在user_info_ext 对已使用年假加计数
            if($data['type'] == '年假') {
                apf_require_class('Bll_Attendance_UserExt');
                $uext = new Bll_Attendance_UserExt();
                $back = $uext->update_used_annual($data['user_id'], $data['leave_time']);
            }
            //直接写入一级领导的审批记录，更新audit_list
            /*$audit_list = array('uid'=>$data['r_user_id'],'leave_id'=>$rs,'status'=>1);
            if ($data['leave_reason'] == '地铁故障')
            {
                $audit_list['status'] = 2;
            }
            $this->replace_record($audit_list);*/
            $flag = true;
            //审批列表
            $audit_url = Tools_OAUrls::get_instance()->build_audit_list();
            $this->passurl = "<a href ='".$audit_url."'>审批</a>(".$audit_url.")";
            // TODO 发邮件
            apf_require_class('Bll_Attendance_ApplyType');
            if( in_array($data['type'],Bll_Attendance_ApplyType::get_delay_type()) || in_array($data['type'],Bll_Attendance_ApplyType::get_delay_type_2())){
                $title =  $data['leave_reason']."说明 - ".$_SESSION['name'];
                $body  =  "姓名:".$_SESSION['name']."<br>".
                        "工号:".$_SESSION['code']."<br>".
                        "日期:".$data['start_time']."<br>".
                        "原因:".$data['leave_reason'].$data['leave_desc']."<br>".
                        "请点击以下链接审批"."<br>".$this->get_apply_url($rs)."<br>".
                        "如需查看更多,请访问<br>".$this->passurl;
            }else if( in_array($data['type'],Bll_Attendance_ApplyType::get_leave_type()) ){
                $title =  $data['type']."申请 - ".$_SESSION['name'];
                $body  = "姓名:".$_SESSION['name']."<br>".
                        "工号:".$_SESSION['code']."<br>".
                        "起始日期:".$data['start_time']."<br>".
                        "结束日期:".$data['end_time']."<br>".
                        "请假时长:".$data['leave_time']."天<br>".
                        "请假类型:".$data['type']."<br>".
                        "请假理由:".$data['leave_reason'].$data['leave_desc']."<br>".
                        "工作代理人:".$data['leave_agent']."<br>".
                        "请点击以下链接审批"."<br>".$this->get_apply_url($rs)."<br>".
                        "如需查看更多,请访问<br>".$this->passurl;
            }else if($data['type'] == '公出'){
                $title =  "公出申请 - ".$_SESSION['name'];
                 $body  = "姓名:".$_SESSION['name']."<br>".
                        "工号:".$_SESSION['code']."<br>".
                        "起始日期:".$data['start_time']."<br>".
                        "结束日期:".$data['end_time']."<br>".
                        "公出时长:".$data['leave_time']."小时<br>".
                        "公出理由:".$data['leave_reason'].$data['leave_desc']."<br>".
                        "请点击以下链接审批"."<br>".$this->get_apply_url($rs)."<br>".
                        "如需查看更多,请访问<br>".$this->passurl;
            }else{
                $title =  "考勤申请审核";
                $body  = "请点击以下链接审批"."<br>".$this->passurl;
            }

            apf_require_class("Tools_SendMail");

            /*
            if(!empty($data['r_user_id']) && !empty($data['b_user_id']) ){
                $row = $this->get_userinfo_dao()->find_by_id($data['b_user_id']);
                if ($row['code'] == 'SHA001'){
                    //不发送邮件
                }else
                {
                    //发送邮件
                    $tools = Tools_SendMail::get_instance()->send($row['email'], $title, $body);
                }
            }*/
/*
            if (!empty($data['b_user_id'])) {
                $row_b = $this->get_userinfo_dao()->find_by_id($data['b_user_id']);
                if ($row_b['code'] != 'SHA001') {
                    //发送邮件
                    $tools = Tools_SendMail::get_instance()->send($row_b['email'], $title, $body);
                }
            }*/
            $row_r = $this->get_userinfo_dao()->find_by_id($data['r_user_id']);
            $tools = Tools_SendMail::get_instance()->send($row_r['email'], $title, $body);

        }
        return $flag;
    }

    public function upload_img($record_id, $img) {
        if(empty($img) || empty($record_id)) {
            return false;
        }
        return $this->get_default_dao()->update_by_id($record_id, array(
                'img' => $img
        ));
    }

    /**
     *请假记录写入到记录列表
     * @param array $date('uid'=>$date['uid'],'leave_id'=>$date['leave_id'],'status'=>$date['staus']);
     */
    public function replace_record($date){
        $sql = "REPlACE INTO audit_list(uid,leave_id,status) values('{$date['uid']}','{$date['leave_id']}','{$date['status']}')";
        return $this->get_default_dao()->execute($sql);
    }
    /**
     * 申请记录只有一条
     * @param unknown_type $date
     */
    public function is_ever_apply($date){
        $where = array(
            'user_id'   => $date['user_id'],
            'start_time'=> $date['start_time'],
            'end_time'  => $date['end_time'],
            'type'      => $date['type']
        );
       $temp = $this->get_default_dao()->find($where);
       if(empty($temp)){
           return true;
       }else{
           if($temp[0]['status']=='已驳回' || $temp[0]['status']=='已撤销' ){
               return true;
           }
           return false;
       }
    }
    private function get_apply_url($aid){
        $url = Tools_OAUrls::get_instance()->build_leave_detail_url($aid);
        return "<a href ='".$url."'>".$url."</a>";
    }
    /*
     * @return Dao_Attendance_LeaveRecord
     */
    private function get_default_dao() {
        return $this->get_dao('Dao_Attendance_LeaveRecord');
    }

    private function get_tripdetails_dao() {
        return $this->get_dao('DAO_Attendance_TripDetail');
    }

    private function get_tripmanager_dao() {
        return $this->get_dao('DAO_Attendance_TripManager');
    }

    /**
     * return DAO_Attendance_UserInfo
     */
    private function get_userinfo_dao(){
        return $this->get_dao('DAO_Attendance_UserInfo');
    }
}
